/**
 * Copyright (c) 2010-2015 EEFUNG Software Co.Ltd. All rights reserved.
 * 版权所有(c)2010-2015湖南蚁坊软件有限公司。保留所有权利。
 */
package com.milan.analysis.common.remoting;

import com.milan.analysis.common.extend.RestRequestCallback;
import com.milan.analysis.common.interceptor.AuthInterceptor;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.CharEncoding;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.ResponseExtractor;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * TongjiPreLoginClient
 *
 * @author liaozhidan
 * @date 2017/3/12
 */

public class TongjiClient extends RemotingBase{

    private static final Logger logger = LoggerFactory.getLogger(TongjiClient.class);

    public static final String ADDRESS = "https://api.baidu.com/sem/common/HolmesLoginService";


    public String perLogin() throws Exception{
        try {
            MultiValueMap<String, String> requestArgs = new LinkedMultiValueMap<String, String>(4);

            requestArgs.set("osVersion", "0.1");
            requestArgs.set("deviceType", "cellphone");
            requestArgs.set("clientVersion", "Android");


            Map<String, String> sendHeaders = new HashMap<String, String>();

            // sendHeaders.put("realm", clientSupport.getRealm());
//            sendHeaders.put("session_id", sessionId);
            sendHeaders.put("UUID", "123");
            sendHeaders.put("account_type", "1");

            return restTemplate.execute(ADDRESS, HttpMethod.POST,
                                        new RestRequestCallback(restTemplate, sendHeaders, MediaType.APPLICATION_FORM_URLENCODED,
                                                                MediaType.APPLICATION_JSON, requestArgs),
                                        new ResponseExtractor<String>() {
                                            @Override
                                            public String extractData(ClientHttpResponse response)
                                                throws IOException {
                                                String json = IOUtils.toString(response.getBody(), CharEncoding.UTF_8);
                                                return json;
                                            }
                                        }, new Object[] {});

        } catch (Exception ex) {
            logger.error("用户登录错误! {}", ex.getMessage());
            throw new RuntimeException(ex.getMessage(), ex);
        }
    }
}
